//
//  LineViewController.swift
//  FrameProduct
//
//  Created by delegate on 2025/10/21.
//

import UIKit

class LineViewController: BaseViewController {
    // 数据源（使用NoteSectionModel数组）
    private var sections: [NoteSectionModel] = []
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupNavi()
        setupUI()
        loadTestData()
    }
    private func setupNavi() {
        self.navigationItem.titleView = naviTitleView
    }
    private func setupUI() {
        view.addSubview(tableView)
        tableView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
    }
    // 加载测试数据
    private func loadTestData() {
        // 模拟3个section数据
        for i in 1...3 {
            var notes: [NoteModel] = []
            // 每个section包含3条笔记
            for j in 1...3 {
                notes.append(NoteModel(
                    selected: j % 2 == 0,
                    noteLineTitle: "笔记标题\(i)-\(j)",
                    noteLineSubTitle: "笔记副标题\(i)-\(j)",
                    noteSpeed: "\(j*10)km/h",
                    noteSpeedPercent: j*20,
                    collected: j % 3 == 0
                ))
            }
            
            sections.append(NoteSectionModel(
                countryName: "国家\(i)",
                countryIcon: "country\(i)", // 假设图片资源名为country1, country2等
                countrySpeed: "\(i*50)km/h",
                isOpen: i == 1, // 默认第一个section展开
                noteModel: notes
            ))
        }
        tableView.reloadData()
    }
    //=================================================================
    //                           事件处理
    //=================================================================
    // MARK: - 事件处理
    private func normalNoteDidTapAction() {
        
    }
    private func advancedNoteDidTapAction() {
        
    }
    //=================================================================
    //                            lazy
    //=================================================================
    // MARK: - lazy
    private lazy var naviTitleView: LineNaviCustomView = {
        let navi = LineNaviCustomView()
        navi.normalNoteButtonActionBlock = {[weak self] in self?.normalNoteDidTapAction()}
        navi.advancedNoteButtonActionBlock = {[weak self] in self?.advancedNoteDidTapAction()}
        return navi
    }()
    private lazy var tableView: UITableView = {
        let tableView = UITableView(frame: .zero, style: .grouped)
        tableView.separatorStyle = .none
        tableView.delegate = self
        tableView.dataSource = self
        tableView.registerCellClass(LineTableViewTableViewCell.self)
        tableView.backgroundColor = .clear
        return tableView
    }()
}
extension LineViewController: UITableViewDelegate, UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        sections.count
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        let section = sections[section]
        return section.isOpen ? section.noteModel.count: 0
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(LineTableViewTableViewCell.self, for: indexPath)
        let note = sections[indexPath.section].noteModel[indexPath.row]
        cell.configure(with: note, isHiddenBottomLineView: (sections[indexPath.section].noteModel.count - 1) == indexPath.row)
        cell.collectedDidTapActionBlock = { [weak self] currentNote in
            self?.sections[indexPath.section].noteModel[indexPath.row].collected.toggle()
            self?.tableView.reloadRows(at: [indexPath], with: .automatic)
        }
        cell.selectedDidTapActionBlock = { [weak self] currentNote in
            self?.sections[indexPath.section].noteModel[indexPath.row].selected.toggle()
            self?.tableView.reloadRows(at: [indexPath], with: .automatic)
        }
        return cell
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        tableView.rowHeight
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        58
    }
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let headerView = LineTableViewHeaderView(frame: CGRect(x: 0, y: 0, width: ScreenSize.width, height: 58))
        headerView.headerViewDidTapActionBlock = { [weak self] sectionModel in
            self?.sections[section].isOpen.toggle()
            self?.tableView.reloadSections(IndexSet(integer: section), with: .automatic)
        }
        headerView.sectionModel = sections[section]
        return headerView
    }
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        nil
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        0
    }
}
